<template>
<div>
    <div class="seat-map" :style="{width:mapWidth+'px'}" ref="seatMap">
        <div class="seat-wrap"
            :style="{width: (50+map.maxCol*50) + 'px'}">
            <!-- seat-screen -->
            <div class="seat-screen"><b>影厅荧幕</b></div>
            <!-- seat-nav -->
            <div class="seat-nav"><b v-for="(row,x) in seatsMap" :key="'idx'+x">{{row.idx}}</b></div>
            <!-- seat-row -->
            <div v-for="(row,x) in seatsMap" :key="'r'+x" class="seat-row">
                <span v-for="(item,y) in row.seats" :key="'c'+y" 
                    :class="'seat-slot seat'+item.flag"
                    :title="item.name"
                    @click="toggle(item)">
                </span>
            </div>
        </div>
    </div>
    <div class="seat-info">
        <div v-if="selected.length > 0"><small>已选座位: {{selected.length}}</small></div>
        <span v-for="(item,idx) in selected" :key="'ss'+idx">
            {{item}}
        </span>
    </div>
</div>
</template>



<style lang="stylus">
.seat-map {
  overflow:scroll;
  height:250px;
  border: 1px dashed #aaa;
  background-color: #f2f1f6;
}
.seat-wrap {
  /*border:1px solid red;*/
}
.seat-row {
  white-space: nowrap;
  padding-left:40px;
}
.seat-slot {
  display:inline-block;
  position: relative;
  width: 40px;
  height: 40px;
  margin: 5px;
  text-align: center;
  background-size: contain;
  color: #fff;
  background-repeat: no-repeat;
}
/* 1:可选;0:已被选;2:被选中;9:过道; */
.seat0 {
  background-image: url("");
}
.seat1 {
  background-image: url("");
}
.seat2 {
  background-image:url("")!important;
}

.seat-nav {
    position:absolute;
    left:0;
    top:85px;
    background-color: #aaa;
    color:#fff;
    border-radius: 10px;
}
.seat-nav>b {
    display:block;
    line-height:54px;
    height:54px;
    width:30px;
    text-align:center;
    z-index: 100;
}
.seat-screen {
    text-align: center;
    padding-bottom:50px;
}
.seat-screen>b {
    display: inline-block;
    font-size: 25px;
    width: 400px;
    background-color: #e3e3e3;
    border-radius: 0 0 30px 30px;
    z-index: 99;
}
.seat-info {
    min-height: 80px;
}
.seat-info span {
    display: inline-block;
    border: 1px dashed #aaa;
    padding: 5px 10px;
    margin: 5px;
}
</style>

<script>

import BScroll from 'better-scroll'

export default {
  name: 'MySeatMap',
  data() {
    return {
      //map:false,
      scroll:false
    }
  },
  mounted() {
    let me = this;
    let scale = me.initScale;
    let start = (scale + 0.005).toFixed(2);
    setTimeout(function(){
      me.scroll = new BScroll('div.seat-map', {
          scrollX:true,
          scrollY:true,
          click:true,
          zoom: {start:start,min:scale,max:1}
      })
    }, 500);
    //me.$nextTick(() => {})
  },
  methods: {
    toggle(item) {
      if(item.flag == 1) {
        // 进行选中操作
        let seatInfo = this.map.seatInfos[item.x+''].details[item.y];
        seatInfo.available = 2;
        //console.log(seatInfo);
        this.selected.push(item.name);
      } else if(item.flag == 2) {
        // 进行取消操作
        let seatInfo = this.map.seatInfos[item.x+''].details[item.y];
        seatInfo.available = 1;
        //console.log(seatInfo);
        let idx = this.selected.indexOf(item.name);
        if(idx >= 0) {
            this.selected.splice(idx, 1);
        }
      }  
      //console.log(this.selected);    
    }
  },
  computed: {
    initScale() {
      let s = this.mapWidth / (50+this.map.maxCol*50);
      return s > 1 ? 1 : s;
    },
    mapWidth() {
      if(this.$q.platform.is.mobile) {
        return document.body.clientWidth;
      } else {
        return document.body.clientWidth - 100 - (this.$api.ui.drawer.open?250:0);
      }
    },
    seatsMap() {
      if(!this.map) return [];
      let seatInfos = this.map.seatInfos;

      // 判断 x,y 起始坐标，大部分从1开始，有个别操蛋影院从0开始;
      let firstX = 1;
      let firstY = 1;
      for(let k in this.map.seatInfos) {
        let seatRow = seatInfos[k];
        if(seatRow.x < firstX) {
          firstX = seatRow.x;
        }
        for(let idx in seatRow.details) {
          let seatInfo = seatRow.details[idx];
          if(seatInfo.y < firstY) {
            firstY = seatInfo.y;
          }
        }
      }
      
      // x,y坐标以0开始时，最大行列数会少1，草草草
      let maxRow = this.map.maxRow + 1 - firstX;
      let maxCol = this.map.maxCol + 1 - firstY;

      // 初始化自定义座位图数据结构
      let seatsMap = [];
      for(let i = 0; i < maxRow; i++) {
        let row = {idx:'', seats:[]};
        for(let j = 0; j < maxCol; j++) {
          row.seats.push({'x':'','y':'','flag':9,name:''});
        }
        seatsMap.push(row);
      }

      for(let k in this.map.seatInfos) {
        let seatRow = seatInfos[k];
        let x = seatRow.x - firstX;  // 从1开始==>从0开始
        seatsMap[x].idx = seatRow.desc;
        for(let idx in seatRow.details) {
          let seatInfo = seatRow.details[idx];
          let y = seatInfo.y - firstY; // 从1开始==>从0开始
          seatsMap[x].seats[y].name = seatInfo.name;
          seatsMap[x].seats[y].flag = seatInfo.available;
          seatsMap[x].seats[y].x = k;
          seatsMap[x].seats[y].y = idx;
          let containIdx = this.selected.indexOf(seatInfo.name);
          if(containIdx >= 0) {
              seatsMap[x].seats[y].flag = 2;
          }
        }
      }
      return seatsMap;
    }
  },
  props: {
    map: {
      type: Object,
      required: true      
    },
    selected: {
      type: Array,
      required: false,
      default: function (){return [];}      
    },
    mode: {
      type: Number,
      required: false,
      default: 0,  
    }
  }
}

</script>
